<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
/**
 * 好动系统扩展数据
 * */
class HdExtra extends AdminBase{
	function _initialize() {
		parent::_initialize();  
    }
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function index(){
		echo $this->fetch();
	}	
	function add(){
		echo $this->fetch();
	}
	/*新增*/
	function add_post(){
		if(request()->isPost()){
			$card_no=input("card_no");
			$this->varifyuniq($card_no);
			$shop_id=sp_get_current_shop_id();
			$data['card_no']=$card_no;
			$data['face1']=input("face1");
			$data['icon1']=input("icon1");
			$data['shop_id']=$shop_id;
			$data['brand_id']=db("shop")->where("id=$shop_id")->value("parent_id");
			$data["face_verify"]=input("face_verify");
			$data["name"]=input("name");
			$data["mobile"]=input("mobile");
			$data["down_key"]=input("down_key");
			db("HdExtra")->insert($data);			
			$this->ajax_success("成功!");	
		}else{
			$this->ajax_error("非法操作!");
		}
	}
	function varifyuniq($card_no){
		 $count=db("HdExtra")->where("card_no='$card_no'")->count(1);
		 if(!empty($count)){
		 	$this->ajax_error("卡号重复");
		 }
	}
	/*修改信息*/
	function edit(){
		$id=input("id/d");
		$model=db("HdExtra")->find($id);
		if(empty($model['icon1'])){
			$model["face_image1"]="/data/finger/a8.jpg";
		}else{
			$model["face_image1"]=$model['icon1'];
		}
		$this->assign("model",$model);
		
		echo $this->fetch();			
	}
	function edit_post(){
		if(request()->isPost()){
			$card_no=input("card_no");
			$old_card_no=input("old_card_no");
			$this->has_use($card_no,$old_card_no);
			$data["id"]=input("id");
			$data['card_no']=$card_no;
			$data['face1']=input("face1");
			$data['icon1']=input("icon1");
			$data["face_verify"]=input("face_verify");
			$data["name"]=input("name");
			$data["mobile"]=input("mobile");
			$data["down_key"]=input("down_key");
			db("HdExtra")->update($data);			
			$this->ajax_success("成功!");	
		}else{
			$this->ajax_error("非法操作!");
		}
	}
	function has_use($card_no,$old_card_no){
		$count=db("HdExtra")->where("card_no='$card_no' and card_no!='$old_card_no'")->count(1);
		 if(!empty($count)){
		 	$this->ajax_error("卡号重复");
		 }
	}
	function delete(){
		$id=input("id/d");
		db("HdExtra")->delete($id);
		$this->ajax_success();
	}
	
	/*列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10041018");
		$this->assign("menus",$auth_group);
	}
	
	/*获取数据*/
	private function _ajax_index(){
		$query=array('card_no'=>array("field" =>"card_no","operator" =>"="),
					 'mobile'=>array("field" =>"mobile","operator" =>"="),
					 'name'=>array("field" =>"name","operator" =>"="),
					 );
		$shop_id=sp_get_current_shop_id();	
		$draw=input("draw",0)+1;//请求时间	
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));		 
		$where_and.=" and brand_id=(select parent_id from m_shop where id=$shop_id)";
		
		$total=db("HdExtra")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("HdExtra")->field("id,card_no,icon1,shop_id,brand_id,face_verify,name,mobile")->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
		
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
}
